package org.hy.service.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hy.domain.group.GAnswer;
import org.hy.domain.group.GDiscuss;
import org.hy.domain.group.GMember;
import org.hy.domain.ug.Counter;
import org.hy.persistence.group.GDiscussMapper;
import org.hy.util.AppConstant;
import org.hy.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 讨论接口实现类
 * @author Administrator
 *
 */
@Service
public class GDiscussServiceImpl implements IGDiscussService {

	@Autowired
	private GDiscussMapper gdMapper;
	
	@Override
	@Transactional
	public int addGDiscuss(GDiscuss discuss) {
		if(discuss == null)return AppConstant.FAIL;
		return gdMapper.insertGDiscuss(discuss);		
	}

	@Override
	@Transactional
	public int addAnswer(GAnswer answer) {
		if(answer == null)return AppConstant.FAIL;
		return gdMapper.insertGAnswer(answer);
	}

	@Override
	public int updateGDiscuss(GDiscuss discuss) {
		if(discuss == null)return AppConstant.FAIL;
		return gdMapper.updateGDiscuss(discuss);
	}

	@Override
	public int updateGAnswer(GAnswer answer) {
		if(answer == null)return AppConstant.FAIL;
		return gdMapper.updateGAnswer(answer);
	}

	@Override
	@Transactional
	public int deleteGDiscuss(int id, int ownerIdGm) {
		GDiscuss dis = gdMapper.findGDiscussById(id);
		GMember actualOwner = (GMember)dis.getPoster();
		if(actualOwner.getId() != ownerIdGm) throw AppConstant.ILLEGAL_REMOVE;
		return gdMapper.deleteGDiscuss(id);
	}

	@Override
	@Transactional
	public int deleteGAnswer(int id, int ownerIdGm) {
		GAnswer answer = gdMapper.findGAnswerById(id);
		GMember actualOwner = (GMember)answer.getPoster();
		int discussId = answer.getRefFeedId() | answer.getIdGdiscuss();
		GDiscuss dis = gdMapper.findGDiscussById(discussId);
		GMember parentOwner = (GMember)dis.getPoster();
		if(ownerIdGm == actualOwner.getId() || ownerIdGm == parentOwner.getId()){
			return gdMapper.deleteGAnswer(id);
		}
		return AppConstant.FAIL;
	}

	@Override
	public List<GDiscuss> findGDiscussWithGAnswerByGid(int gid, int page,
			int size) {
		int offset = Pager.getOffset(page, size);
		return gdMapper.findGDiscussWithGAnswerByGid(gid, offset, size);
	}

	@Override
	public GDiscuss findGDiscussWithGAnswerById(int id, int page, int size) {
		int offset = Pager.getOffset(page, size);
		return gdMapper.findGDiscussWithGAnswerById(id, offset, size);
	}
	
	@Override
	public GAnswer findAnswerById(int id) {
		return gdMapper.findGAnswerById(id);
	}

	@Override
	public GDiscuss findGDiscussById(int id) {
		return gdMapper.findGDiscussById(id);
	}

	@Override
	public int countGDiscuss(int gid) {
		return gdMapper.countGDiscuss(gid);
	}

	@Override
	public Map<Integer,Integer> countGAnswer(int[] gDiscussIds) {
		if(gDiscussIds == null || gDiscussIds.length == 0)return null;
		List<Counter> counterList =  gdMapper.countGAnswer(gDiscussIds);
		Map<Integer,Integer> retMap = new HashMap<Integer,Integer>();
		for (Counter counter : counterList) {
			retMap.put(counter.getId(), counter.getNum());
		}
		for (Integer id : gDiscussIds) {
			if(!retMap.containsKey(id)){
				retMap.put(id, 0);
			}
		}
		return retMap;
	}

	@Override
	public int countWisdomedGDiscuss(int gid) {
		return gdMapper.countWisdomedGDiscuss(gid);
	}

	@Override
	public List<GDiscuss> findWisdomedGDiscuss(int gid, int page, int size) {
		int offset = Pager.getOffset(page, size);
		return gdMapper.findWisdomedGDiscuss(gid, offset, size);
	}

	
	
}
